#include "mysql_fun.h"
#include "re_chck.h"

extern MYSQL_STRUC mys_stc;

void initial_mysql(MYSQL_STRUC *mq_st)
{
	mq_st->server = "192.168.9.57";
	mq_st->user = "admin";
	mq_st->password = "123456";
	mq_st->database = "gnss";

	mq_st->conn = mysql_init(NULL);

	if(!mysql_real_connect(mq_st->conn, mq_st->server, mq_st->user, mq_st->password, mq_st->database, 0, NULL, 0)){
		fprintf(stderr, "%s\n", mysql_error(mq_st->conn));
		exit(1);
	}
	mysql_query(mq_st->conn, "set names gbk");
}

//terminal login
INT32 term_reg_fun(TERM_REG_FM *tm, INT8 *sim)
{
	char cmd[STR_LEN];
	INT32 r = 0x0;
//	MYSQL_STRUC mys_stc;

//	initial_mysql(&mys_stc);

	E(" producer: %s. type1: %s. no1: %s.", tm->producer, tm->type1, tm->no1);
	E("province: %d. city: %d. color: %d. license: %s", tm->province, tm->city, tm->color, tm->license);

	memset(cmd, 0, sizeof(cmd));
	sprintf(cmd, "select *from recorder where producer = '%s'	\
			&& type1 = '%s' && no1 = '%s';", tm->producer, tm->type1, g_installArgs.tmlId);

	E(" CMD: %s.", cmd);
	if(mysql_query(mys_stc.conn, cmd)){
		fprintf(stderr, "%s\n", mysql_error(mys_stc.conn));
		exit(1);
	}
	mys_stc.res = mysql_use_result(mys_stc.conn);

	if((mys_stc.row = mysql_fetch_row(mys_stc.res)) == NULL){
		E(" Terminal not exsit in DB." );
		r = 0x04;
	}else{
		mysql_free_result(mys_stc.res);

		memset(cmd, 0, sizeof(cmd));
		sprintf(cmd, "select *from resister where no1 = '%s'	\
				&& producer = '%s' && type1 = '%s';", tm->no1, tm->producer, tm->type1);
		if(mysql_query(mys_stc.conn, cmd)){
			fprintf(stderr, "%s\n", mysql_error(mys_stc.conn));
			exit(1);
		}
		mys_stc.res = mysql_use_result(mys_stc.conn);
		if((mys_stc.row = mysql_fetch_row(mys_stc.res))){
			E(" Terminal was registered." );
			r = 0x03;
		}else{
			mysql_free_result(mys_stc.res);

			memset(cmd, 0, sizeof(cmd));
			sprintf(cmd, "select *from vehicle where province = '%d' && city = '%d'	\
					&& color = '%d' && lisence = '%s';", tm->province, tm->city, tm->color, tm->license);
			if(mysql_query(mys_stc.conn, cmd)){
				fprintf(stderr, "%s\n", mysql_error(mys_stc.conn));
				exit(1);
			}
			mys_stc.res = mysql_use_result(mys_stc.conn);
			if((mys_stc.row = mysql_fetch_row(mys_stc.res)) == NULL){
				E(" Vehicle not exsit in DB." );
				r = 0x02;
			}else{
				mysql_free_result(mys_stc.res);
				memset(cmd, 0, sizeof(cmd));
				sprintf(cmd, "select *from resister where city = '%d' && color = '%d'	\
						&& license = '%s' && province = '%d';", tm->city, tm->color, tm->license, tm->province);
				if(mysql_query(mys_stc.conn, cmd)){
					fprintf(stderr, "%s\n", mysql_error(mys_stc.conn));
					exit(1);
				}
				mys_stc.res = mysql_use_result(mys_stc.conn);
				if((mys_stc.row = mysql_fetch_row(mys_stc.res))){
					E(" Vehicle was registered.");
					r = 0x01;
				}else{
					mysql_free_result(mys_stc.res);
					unsigned long rand_num;
					char autuher[20] = {0};

					rand_num = create_auth();
					sprintf(autuher, "%lu", rand_num);

					memset(cmd, 0, sizeof(cmd));
					/* modify resister insert a record.*/
					sprintf(cmd, "insert resister values(null, '%s', '%d', '%d', now(), '%s', '%s', '%s',	\
						'%d', '%s', 1, '%s');", autuher, tm->city, tm->color, tm->license, tm->no1, tm->producer,	\
						tm->province, tm->type1, sim);
					if(mysql_query(mys_stc.conn, cmd)){
						fprintf(stderr, "%s\n", mysql_error(mys_stc.conn));
						exit(1);
					}

					memset(cmd, 0, sizeof(cmd));
					/* modify vehicle update online.*/
					sprintf(cmd, "update vehicle set online = 1 where color = '%d' && lisence = '%s';",	\
							tm->color, tm->license);
					if(mysql_query(mys_stc.conn, cmd)){
						fprintf(stderr, "%s\n", mysql_error(mys_stc.conn));
						exit(1);
					}
					r = 0x0;
				}		
			}
		}
	}	

	mysql_close(mys_stc.conn);
	mys_stc.conn = NULL;

	return r;
}

void get_field(INT8 *cmd, INT8 *str)
{
//	MYSQL_STRUC mys_stc;

//	initial_mysql(&mys_stc);

	if(mysql_query(mys_stc.conn, cmd)){
		fprintf(stderr, "%s\n", mysql_error(mys_stc.conn));
		exit(1);
	}

	mys_stc.res = mysql_use_result(mys_stc.conn);

	while((mys_stc.row = mysql_fetch_row(mys_stc.res)) != NULL)
		memcpy(str, mys_stc.row[0], strlen(mys_stc.row[0]));

	mysql_free_result(mys_stc.res);
	mysql_close(mys_stc.conn);
	mys_stc.conn = NULL;

}

INT32 contrast_Akey(INT8 *cmd, INT8 *str)
{
//	MYSQL_STRUC mys_stc;
	INT8 auth[20] = {0};

//	initial_mysql(&mys_stc);

	if(mysql_query(mys_stc.conn, cmd)){
		fprintf(stderr, "%s\n", mysql_error(mys_stc.conn));
		exit(1);
	}

	mys_stc.res = mysql_use_result(mys_stc.conn);

	while((mys_stc.row = mysql_fetch_row(mys_stc.res)) != NULL)
		memcpy(auth, mys_stc.row[1], strlen(mys_stc.row[1]));

	if(strncmp(auth, str, strlen((const char *)str)))
		return 1;

	mysql_free_result(mys_stc.res);
	mysql_close(mys_stc.conn);
	mys_stc.conn = NULL;
	return 0;
}

INT32 mysql_terMlogout(INT8 *cmd, INT8 *str)
{
//	MYSQL_STRUC mys_stc;

//	initial_mysql(&mys_stc);

	if(mysql_query(mys_stc.conn, cmd)){
		fprintf(stderr, "%s\n", mysql_error(mys_stc.conn));
		exit(1);
	}
	mys_stc.res = mysql_use_result(mys_stc.conn);
	if((mys_stc.row = mysql_fetch_row(mys_stc.res)) == NULL){
		E(" Terminal logout failed.");
		mysql_free_result(mys_stc.res);
		return 1;
	}else{
		mysql_free_result(mys_stc.res);
		memset(cmd, 0, strlen(cmd)+1);
		sprintf(cmd, "delete from resister where sim = '%s';", str);
		if(mysql_query(mys_stc.conn, cmd)){
			fprintf(stderr, "%s\n", mysql_error(mys_stc.conn));
			//TODO:exit or not
			exit(1);
		}
	}
	mysql_close(mys_stc.conn);
	mys_stc.conn = NULL;

	return 0;
}

INT32 mysql_fun(INT8 *cmd)
{
//	MYSQL_STRUC mys_stc;

//	initial_mysql(&mys_stc);

	if(mysql_query(mys_stc.conn, cmd)){
		fprintf(stderr, "%s\n", mysql_error(mys_stc.conn));
		return 1;
	}
	mysql_close(mys_stc.conn);
	mys_stc.conn = NULL;

	return 0;
}


